package com.dk.controller;

import com.dk.Vo.PublisherVo;
import com.dk.pojo.Publisher;
import com.dk.pojo.TCountry;
import com.dk.service.PublisherServiceI;
import com.dk.service.TCountryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("publisherController")
public class PublisherController {

    //出版社
    @Autowired
    private PublisherServiceI publisherServiceI;


    @Autowired
    private TCountryService tCountryService;

    //跳转出版社查询页面
    @RequestMapping("list")
    public String listGet(){
        return "publisher/list";
    }

    //查询出版社页面
    @RequestMapping("getInfoServer")
    @ResponseBody
    public Map<String,Object> getInfoServer(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "2")Integer pageSize){
        PageInfo<PublisherVo> page = publisherServiceI.getInfoServer(pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("rows", page.getList());
        return map;
    }

    //跳转出版社增加页面
    @RequestMapping("insertInfo")
    public String insertInfo(){
        return "publisher/add";
    }

   //查询三级联动
    @RequestMapping("getCityByPid")
    @ResponseBody
    public List<TCountry> getCityByPid(Integer pid){
        List<TCountry> list = tCountryService.getInfo(pid);
        return list;
    }

    //确认增加出版社
    @RequestMapping("addById")
    @ResponseBody
    public int addById(Publisher publisher){
        publisherServiceI.addById(publisher);
        return 1;
    }

    //修改回显
    @RequestMapping("updateInfo/{pubId}")
    public String updateInfo(@PathVariable("pubId")Integer pubId, Model model){

        //根据id 查询出版社
        Publisher publisher =  publisherServiceI.getUpdateInfo(pubId);
        model.addAttribute("publisher",publisher);

        //查询所有国家,根据条件查询
        List<TCountry> guo = tCountryService.getCountry();
        model.addAttribute("guo",guo);
        //根据国查所有的省
        Integer guoId = publisher.getCountry();
        List<TCountry> sheng = tCountryService.getSheng(guoId);
        model.addAttribute("sheng",sheng);
        //根据省查下面的市
        Integer shiId = publisher.getProvince();
        List<TCountry> shi = tCountryService.getShi(shiId);
        model.addAttribute("shi",shi);

        return "publisher/update";
    }

    //确认修改出版社
    @RequestMapping("updateById")
    @ResponseBody
    public int updateById(Publisher publisher){
        publisherServiceI.updateById(publisher);
        return 1;
    }

    //删除出版社
    @RequestMapping("deleteById/{pubId}")
    @ResponseBody
    public int deleteById(@PathVariable("pubId")Integer pubId){
        publisherServiceI.deleteById(pubId);
        return 1;
    }
}
